package com.fishery.base.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fishery.base.entity.BaseSupply;
import com.fishery.base.entity.ConstantValue;
import com.fishery.base.entity.SupplyIn;
import com.fishery.base.entity.SupplyOut;
import com.fishery.base.entity.vo.BaseSupplyAccessVo;
import com.fishery.base.entity.vo.BaseSupplyAmountVo;
import com.fishery.base.entity.vo.BaseSupplyNameVo;
import com.fishery.base.service.BaseSupplyService;
import com.fishery.base.service.SupplyInService;
import com.fishery.base.service.SupplyOutService;
import com.fishery.entity.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 基地投入品前端控制器
 * </p>
 *
 * @author Ghost
 * @since 2021-01-21
 */
@RestController
@RequestMapping("/base/supply")
@Api(tags = "基地投入品管理")
@CrossOrigin
public class BaseSupplyController {

	@Autowired
	private SupplyInService supplyInService;

	@Autowired
	private SupplyOutService supplyOutService;

	@Autowired
	private BaseSupplyService baseSupplyService;


	@PostMapping("/in")
	@ApiOperation("创建投入品入库记录")
	public Result addSupplyIn(@RequestBody @ApiParam(value = "投入品入库信息对象") SupplyIn supplyIn) {
		if (supplyIn == null
				|| StringUtils.isEmpty(supplyIn.getBaseId())
				|| StringUtils.isEmpty(supplyIn.getSupplyId())
				|| StringUtils.isEmpty(supplyIn.getSupplyName())
				|| StringUtils.isEmpty(supplyIn.getOperatorName())
				|| StringUtils.isEmpty(supplyIn.getSupplyTypeName())
				|| supplyIn.getInWeight().compareTo(ConstantValue.ZERO) <= 0) {
			return Result.error("入库失败");
		}
		boolean ret = supplyInService.createSupplyInRecord(supplyIn);
		return ret ? Result.success("入库成功") : Result.error("入库失败");
	}

	@PutMapping("/in")
	@ApiOperation("修改投入品入库记录")
	public Result updateSupplyIn(@RequestBody @ApiParam(value = "投入品入库信息对象") SupplyIn supplyIn) {
		if (supplyIn == null
				|| StringUtils.isEmpty(supplyIn.getId())
				|| supplyIn.getInWeight().compareTo(ConstantValue.ZERO) < 0) {
			return Result.error("修改失败");
		}
		boolean ret = supplyInService.updateSupplyInRecord(supplyIn);
		return ret ? Result.success("修改成功") : Result.error("修改失败");
	}


	@GetMapping("/in/{baseId}/{size}/{current}")
	@ApiOperation("获取投入品入库记录（带分页）")
	public Result pageSupplyIn(@PathVariable(name = "baseId", required = true) @ApiParam(required = true, value = "基地Id") String baseId,
							   @PathVariable(name = "size", required = true) @ApiParam(value = "尺寸", required = true) int size,
							   @PathVariable(name = "current", required = true) @ApiParam(value = "页码", required = true) int current) {
		if (StringUtils.isEmpty(baseId)) {
			return Result.error("查询失败");
		}
		Page<SupplyIn> page = supplyInService.pageSuppyInRecords(baseId, size, current);
		return Result.success().data(page);
	}

	@GetMapping("/in/{baseId}/{supplyId}/{size}/{current}")
	@ApiOperation("查询某种投入品的入库记录（带分页）")
	public Result pageOneSupplyIn(@PathVariable(name = "baseId", required = true) @ApiParam(required = true, value = "基地Id") String baseId,
								  @PathVariable(name = "supplyId", required = true) @ApiParam(required = true, value = "投入品Id") String supplyId,
								  @PathVariable(name = "size", required = true) @ApiParam(value = "尺寸", required = true) int size,
								  @PathVariable(name = "current", required = true) @ApiParam(value = "页码", required = true) int current) {
		if (StringUtils.isEmpty(baseId) || StringUtils.isEmpty(supplyId)) {
			return Result.error("查询失败");
		}
		Page<SupplyIn> page = supplyInService.pageSuppyInRecords(baseId, supplyId, size, current);
		return Result.success().data(page);
	}


	@DeleteMapping("/in/{id}")
	@ApiOperation("通过id删除投入品入库记录")
	public Result deleteSupplyIn(@PathVariable(name = "id", required = true) @ApiParam(value = "id", required = true) String id) {
		if (StringUtils.isEmpty(id)) {
			return Result.error("删除失败");
		}
		boolean ret = supplyInService.removeById(id);
		return ret ? Result.success("删除成功") : Result.error("删除失败");
	}


	@PostMapping("/out")
	@ApiOperation("创建投入品出库记录")
	public Result addSupplyOut(@RequestBody @ApiParam(value = "投入品出库信息对象") SupplyOut supplyOut) {
		if (supplyOut == null
				|| StringUtils.isEmpty(supplyOut.getBaseId())
				|| StringUtils.isEmpty(supplyOut.getSupplyId())
				|| StringUtils.isEmpty(supplyOut.getOperatorName())
				|| StringUtils.isEmpty(supplyOut.getSupplyName())
				|| StringUtils.isEmpty(supplyOut.getSupplyTypeName())
				|| supplyOut.getOutWeight().compareTo(ConstantValue.ZERO) <= 0) {
			return Result.error("出库失败");
		}
		boolean ret = supplyOutService.createSupplyOutRecords(supplyOut);
		return Result.success("出库成功");
	}

	@DeleteMapping("/out/{id}")
	@ApiOperation("通过id删除投入品出库记录")
	public Result deleteSupplyOut(@PathVariable(name = "id", required = true) @ApiParam(required = true, value = "id") String id) {
		if (StringUtils.isEmpty(id)) {
			return Result.error("删除失败");
		}
		boolean ret = supplyOutService.removeById(id);
		return ret ? Result.success("删除成功") : Result.error("删除失败");
	}

	@GetMapping("/out/{baseId}/{size}/{current}")
	@ApiOperation("获取投入品出库记录（带分页）")
	public Result pageSupplyOut(@PathVariable(name = "baseId", required = true) @ApiParam(required = true, value = "基地ID") String baseId,
								@PathVariable(name = "size", required = true) @ApiParam(required = true, value = "尺寸") int size,
								@PathVariable(name = "current", required = true) @ApiParam(required = true, value = "页码") int current) {
		if (StringUtils.isEmpty(baseId)) {
			return Result.error("查询失败");
		}
		Page<SupplyOut> page = supplyOutService.pageSupplyOutRecords(baseId, size, current);
		return Result.success().data(page);
	}

	@GetMapping("/out/{baseId}/{supplyId}/{size}/{current}")
	@ApiOperation("查询某种投入品的出库记录（带分页）")
	public Result pageOneSupplyOut(@PathVariable(name = "baseId", required = true) @ApiParam(required = true, value = "基地Id") String baseId,
								   @PathVariable(name = "supplyId", required = true) @ApiParam(required = true, value = "投入品Id") String supplyId,
								   @PathVariable(name = "size", required = true) @ApiParam(value = "尺寸", required = true) int size,
								   @PathVariable(name = "current", required = true) @ApiParam(value = "页码", required = true) int current) {
		if (StringUtils.isEmpty(baseId) || StringUtils.isEmpty(supplyId)) {
			return Result.error("查询失败");
		}
		Page<SupplyOut> page = supplyOutService.pageSupplyOutRecords(baseId, supplyId, size, current);
		return Result.success().data(page);
	}

	@PutMapping("/out")
	@ApiOperation("修改投入品出库记录")
	public Result updateSupplyOut(@RequestBody @ApiParam(value = "投入品出库信息对象") SupplyOut supplyOut) {
		if (supplyOut == null
				|| StringUtils.isEmpty(supplyOut.getId())
				|| supplyOut.getOutWeight().compareTo(ConstantValue.ZERO) < 0) {
			return Result.error("修改失败");
		}
		boolean ret = supplyOutService.updateSupplyOutRecords(supplyOut);
		return ret ? Result.success("修改成功") : Result.error("修改失败");
	}

	@GetMapping("/{baseId}/{size}/{current}")
	@ApiOperation("查询基地所有投入品的库存信息(带分页)")
	public Result pageBaseSupply(@PathVariable(name = "baseId", required = true) @ApiParam(required = true, value = "基地ID") String baseId,
								 @PathVariable(name = "size", required = true) @ApiParam(required = true, value = "尺寸") int size,
								 @PathVariable(name = "current", required = true) @ApiParam(required = true, value = "页码") int current) {
		if (StringUtils.isEmpty(baseId)) {
			return Result.error("查询失败");
		}
		Page<BaseSupply> page = baseSupplyService.pageBaseSuppy(baseId, size, current);
		return Result.success().data(page);
	}

	@GetMapping("/all/{baseId}")
	@ApiOperation("查询基地所有投入品的库存信息")
	public Result getAllBaseSupplies(@PathVariable(name = "baseId", required = true) @ApiParam(required = true, value = "基地ID") String baseId) {
		if (StringUtils.isEmpty(baseId)) {
			return Result.error("查询失败");
		}
		List<BaseSupply> list = baseSupplyService.listBaseSuppies(baseId);
		return Result.success().data(list);
	}

	@DeleteMapping("/{id}")
	@ApiOperation("删除基地投入品的库存信息")
	public Result deleteBaseSupply(@PathVariable(name = "id", required = true) @ApiParam(required = true, value = "id") String id) {
		if (StringUtils.isEmpty(id)) {
			return Result.error("删除失败");
		}
		boolean ret = baseSupplyService.removeById(id);
		return ret ? Result.success("删除成功") : Result.error("删除失败");
	}

	@GetMapping("/{baseId}")
	@ApiOperation("查看基地投入品的类型(1、渔药，2、饲料)")
	public Result getBaseSupplyType(@PathVariable(name = "baseId", required = true) @ApiParam(required = true, value = "基地ID") String baseId) {
		if (StringUtils.isEmpty(baseId)) {
			return Result.error("查看失败");
		}
		List<Integer> typeList = baseSupplyService.getAllType(baseId);
		return Result.success().data(typeList);
	}

	@GetMapping("/{baseId}/{type}/{size}/{current}")
	@ApiOperation("查看基地某种类型的所有投入品的名字（带分页）")
	public Result getBaseOnTypeSupplies(@PathVariable(name = "baseId", required = true) @ApiParam(required = true, value = "基地ID") String baseId,
										@PathVariable(name = "type", required = true) @ApiParam(required = true, value = "类型") Integer type,
										@PathVariable(name = "size", required = true) @ApiParam(required = true, value = "尺寸") int size,
										@PathVariable(name = "current", required = true) @ApiParam(required = true, value = "页码") int current) {
		if (StringUtils.isEmpty(baseId)) {
			return Result.error("查看失败");
		}
		Page<BaseSupplyNameVo> page = baseSupplyService.pageAllSupplyByTypeAndBaseId(baseId, type, size, current);
		return Result.success().data(page);
	}

	@GetMapping("/in/excel/{baseId}")
	@ApiOperation("将基地投入品入库记录以Excel文件导出")
	public void getExcelOfSupplyInRecords(HttpServletResponse response,
										  @PathVariable(name = "baseId", required = true) @ApiParam(required = true, value = "基地ID") String baseId) {
		if (StringUtils.isEmpty(baseId)) {
			return;
		}
		supplyInService.getSupplyInRecordsByExcel(response, baseId);
	}

	@GetMapping("/out/excel/{baseId}")
	@ApiOperation("将基地投入品出库记录以Excel文件导出")
	public void getExcelOfSupplyOutRecords(HttpServletResponse response,
										   @PathVariable(name = "baseId", required = true) @ApiParam(required = true, value = "基地ID") String baseId) {
		if (StringUtils.isEmpty(baseId)) {
			return;
		}
		supplyOutService.getSupplyOutRecordsByExcel(response, baseId);
	}

	@GetMapping("/amount/{baseId}")
	@ApiOperation("查询基地的投入品库存量、使用量、剩余量信息(供领导仓调用)")
	public Result searchAmountInfoOfBaseSupply(@PathVariable(name = "baseId", required = true) String baseId) {
		if (StringUtils.isEmpty(baseId)) {
			return Result.error("查询失败");
		}
		List<BaseSupplyAmountVo> list = baseSupplyService.getAmountInfoOfBaseSupply(baseId);
		return Result.success().data(list);
	}

	@GetMapping("/amount/in/{baseId}")
	@ApiOperation("查询投入品入库的入库数量(供领导仓调用)")
	public Result searchAmountInfoOfBaseSupplyIn(@PathVariable(name = "baseId", required = true) String baseId) {
		if (StringUtils.isEmpty(baseId)) {
			return Result.error("查询失败");
		}
		List<BaseSupplyAccessVo> list = supplyInService.getAmountInfoOfBaseSupplyIn(baseId);
		return Result.success().data(list);
	}

	@GetMapping("/amount/out/{baseId}")
	@ApiOperation("查询投入品的出库数量(供领导仓调用)")
	public Result searchAmountInfoOfBaseSupplyOut(@PathVariable(name = "baseId", required = true) String baseId) {
		if (StringUtils.isEmpty(baseId)) {
			return Result.error("查询失败");
		}
		List<BaseSupplyAccessVo> list = supplyOutService.getAmountInfoOfBaseSupplyOut(baseId);
		return Result.success().data(list);
	}

}

